<?php

namespace api\admin;

//政策文件类
use utils\base64_to_file;
use utils\oss;

class file
{

    //修改状态
    public function changeStatus($request)
    {
        $id = (int)$request['id'];
        $status = (int)$request['status'];
        $time = time();
        $con = con();
        $res = mysqli_query($con, "update file set status=$status,update_time=$time where id=$id");
        mysqli_close($con);
        if ($res) {
            return out_right();
        } else {
            return out_error();
        }
    }

    //编辑
    public function edit($data){
        $id=(int)$data['id'];
        $title=$data['name'];
        $remark=$data['remark'];
        $time=time();

        $con=con();
        $res=mysqli_query($con,"update file set title='$title',remark='$remark',update_time=$time where id=$id");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error();
        }

    }

    //添加
    public function add($data){
        $title=$data['name'];
        $remark=$data['remark'];
        $img=$data['file'];
        $time=time();

        if(empty($img)){
            return out_error('请至少上传一个文件！');
        }

        $con=con();
        $date=date('Y-m-d');
        $url=[];
        for($i=0;$i<count($img);$i++){
            $file=base64_to_file::getAll("",$img[$i]["file_name"],$img[$i]["file"],$img[$i]['size']);
            $res=oss::upload("file/$date/".$file["name"],$file["path"]);
            $url[]=[
                'name'=>$img[$i]["file_name"],
                'size'=>$img[$i]["size"],
                'url'=>$res["info"]["url"]
            ];
        }
        if(empty($url)){
            mysqli_close($con);
            return out_error('oss出现异常！请联系技术！');
        }
        $file=json_encode($url,JSON_UNESCAPED_UNICODE);

        $res=mysqli_query($con,"insert into file(title,remark,create_time,update_time,file) values('$title','$remark',$time,$time,'$file') ");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error();
        }

    }

    //获取分页列表
    public function getList($data){
        $page=(int)$data['page'];
        $size=(int)$data['size'];
        $start=($page-1)*$size;
        $name=$data['name'];

        $sql="select * from file where is_delete=1 ";
        $limit=" order by `id` DESC  limit $start,$size ";
        if(!empty($name)){
            $sql.=" and title like '%$name%'";
        }
        $con=con();
        $data=mysqli_query($con,$sql.$limit);
        $data=mysqli_fetch_all($data,true);
        $total=mysqli_query($con,$sql);
        $total=mysqli_num_rows($total);
        mysqli_close($con);
        for($i=0;$i<count($data);$i++){
            if(empty($data[$i]['file'])){
                $data[$i]['file']=[];
            }else{
                $file=json_decode($data[$i]['file'],true);
                for($o=0;$o<count($file);$o++){
                    $file[$o]['size']=formatSize($file[$o]['size']);
                }
                $data[$i]['file']=$file;
            }
        }
        return out_right([
            'data'=>$data,
            'page'=>$page,
            'size'=>$size,
            'total'=>$total,
            'total_page'=>ceil($total/$size),
        ]);
    }

    //删除
    public function del($data){
        $id=(int)$data['id'];
        $time=time();
        $con=con();
        $res=mysqli_query($con,"update file set is_delete=0,delete_time=$time where id=$id");
        mysqli_close($con);
        if($res){
            return out_right();
        }else{
            return out_error();
        }
    }

}